package com.weetop.aklyscan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseActivity;
import com.weetop.aklyscan.base.mvp.BasePresenter;
import com.weetop.aklyscan.utils.Tablayout.SegmentTabLayout;
import com.weetop.aklyscan.utils.Tablayout.listener.OnTabSelectListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScanSearchActivity extends BaseActivity {

    @BindView(R.id.tl_1)
    SegmentTabLayout tl1;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.frame_scan_search)
    FrameLayout frameScanSearch;
    private String[] mTitles = {"扫货架坐标", "扫中转箱"};
    private Fragment[] fragments;
    //扫仓库坐标
    private StockPositionFragment stockPositionFragment;
    //扫中转箱
    private ZhuangXiangFragment zhuangXFragment;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_search);
        ButterKnife.bind(this);
        tl1.setTabData(mTitles);
        stockPositionFragment = new StockPositionFragment();
        zhuangXFragment = new ZhuangXiangFragment();

        fragments = new Fragment[]{stockPositionFragment, zhuangXFragment};

        tl1.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switchfragment(fragments[position]);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        switchfragment(stockPositionFragment);
    }

    /**
     * 选择fragment
     *
     * @param fragment
     */
    private void switchfragment(Fragment fragment) {
        // 设置一个默认值
        if (fragment == null) {
            fragment = stockPositionFragment;
        }
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment fragment1 : fragments) {
            if (fragment1 != fragment) {
                if (fragment1 != null && !fragment1.isHidden() && fragment1.isAdded()) {
                    ft.hide(fragment1);
                }
            }
        }

        if (fragment.isAdded() && fragment.isHidden()) {
            ft.show(fragment);
        } else {
            if (!fragment.isAdded()) {
                ft.add(R.id.frame_scan_search, fragment);
            }
        }
        ft.commitAllowingStateLoss();
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}
